package com.z.w;


import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

class Infro{
    public String name;
    public String sex;
    public String phoneNumber;
    public String address;
    public String e_mail;
    static int index = 0;
    static ArrayList<Infro> list = new ArrayList<Infro>();
    static int len = list.size();
    //���캯��
    public Infro(String name,String sex,String phoneNumber,String address,String e_mail){
        this.name = name;
        this.sex = sex;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.e_mail = e_mail;
    }
    public Infro() {
		// TODO Auto-generated constructor stub
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getE_mail() {
		return e_mail;
	}
	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
    public String toString(){
        return " ����"+name+" �Ա�"+sex+" �绰��"+phoneNumber+" ��ַ��"+address+" �����ַ��"+e_mail;
    }

    /*
                            * ��ӹ���
     * */
    public static void addFunction() throws IOException{//��ӹ���
        Infro infro = new Infro("","","","","");
        System.out.println("��������ӵ���ݣ�");
        Scanner in = new Scanner(System.in);
        System.out.print("��������");
        infro.name = in.next();
        System.out.print("�����Ա�");
        infro.sex = in.next();
        System.out.print("����绰��");
        infro.phoneNumber = in.next();
        System.out.print("�����ַ��");
        infro.address = in.next();
        System.out.print("�������䣺");
        infro.e_mail = in.next();
        list.add(index,infro);
        index++;
        if(list.isEmpty()){
            System.out.println("������ʧ����");
        }else{
            System.out.println("�����ӳɹ���");
            len++;//list���ϳ��ȼ�һ
        }
        FileWriter writer = new FileWriter("ͨѶ¼����.txt");
        for(int i = 0 ;i<len;i++){
            String []strwriter = new String[len];
            strwriter[i]=list.get(i).toString();
            writer.write(strwriter[i]);
            writer.write("\r\n");
        }
        System.out.println("�ɹ�д��һ����ݵ� ͨѶ¼����.txt ��");
        writer.close();//�ر�д�������ͷ���Դ
    }
    /*
                            * ɾ����
     * */
     public static void deleteFunction(){
         System.out.print("����Ҫɾ�����ϵ�˵�����:");
         Scanner in_2 = new Scanner(System.in);
         String d1 = in_2.nextLine();
         java.util.Iterator<Infro> it = list.iterator();
         while (it.hasNext()){
               Infro infro = it.next();
               if (infro.name.equals(d1)){
                    it.remove();
                    --index;
                    --len;
               }
         }
         System.out.println("�ɹ�ɾ��һ����ϵ�ˣ�");
         FileWriter writer = null;
 		try {
 			writer = new FileWriter("ͨѶ¼����.txt");
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
          for(int i = 0 ;i<len;i++){
              String []strwriter = new String[len];
              strwriter[i]=list.get(i).toString();
              try {
 				writer.write(strwriter[i]);
 			} catch (IOException e1) {
 				// TODO Auto-generated catch block
 				e1.printStackTrace();
 			}
              try {
 				writer.write("\r\n");
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
          }
          try {
 			writer.close();
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}//�ر�д�������ͷ���Դ
        }
     /*
      *                     �޸Ĺ���
      * */
     public static void reditFunction() throws IOException{
         System.out.print("����Ҫ�޸ĵ�ͨѶ¼������");
         Scanner in_3 = new Scanner(System.in);
         String r1 = in_3.nextLine();
         for(int a = 0; a < len;a++){
             if(r1.equals(list.get(a).name)){
                 System.out.print("�����޸ĺ���Ա�");
                 String sex_1 = in_3.next();
                 list.get(a).sex = sex_1;
                 System.out.print("�����޸ĺ�ĵ绰��");
                 String phoneNumber_1 = in_3.next();
                 list.get(a).phoneNumber = phoneNumber_1;
                 System.out.print("�����޸ĺ�ĵ�ַ��");
                 String address_1 = in_3.next();
                 list.get(a).address = address_1;
                 System.out.print("�����޸ĺ�����䣺");
                 String e_mail_1 = in_3.next();
                 list.get(a).e_mail = e_mail_1;
                 System.out.print("����޸����");
             }
         }
		FileWriter writer = null;
		try {
			writer = new FileWriter("ͨѶ¼����.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         for(int i = 0 ;i<len;i++){
             String []strwriter = new String[len];
             strwriter[i]=list.get(i).toString();
             try {
				writer.write(strwriter[i]);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
             try {
				writer.write("\r\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         }
         try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//�ر�д�������ͷ���Դ
     }
        /*
                                * ��ѯ����
        * */
    public static void searchFunction() throws Exception{//��ѯ����
        System.out.print("������Ҫ��ѯ������");
        Scanner in_4 = new Scanner(System.in);
        String s1=in_4.nextLine();
        for(int a= 0; a<len;a++){
            if(s1.equals(list.get(a).name)){
                System.out.println(list.get(a).toString());
            }
        }
    }

    /*
                            * ��ʾ����
     * */
    public static void showFunction() throws IOException{
    	System.out.println("������ϵ�ˣ�");
        FileReader reader = new FileReader("ͨѶ¼����.txt");
        BufferedReader br = new BufferedReader(reader);
        String str;
        while((str = br.readLine()) != null){//ÿ�ζ�ȡһ���ı����ж��Ƿ񵽴��ļ�β
            System.out.println(str);
        }
        br.close();
    }
    
    /*
     						* ��ȡ����
     * */
    public static void readFunction() throws IOException{
    	 FileReader reader = new FileReader("ͨѶ¼����.txt");
         BufferedReader br = new BufferedReader(reader);
         String lineTxt = null;
			while ((lineTxt = br.readLine()) != null)
			 {
			    Infro infro =new Infro("","","","","");
			     String[] a= lineTxt.split("[��\\s+]");
			     infro.name=a[2];
			     infro.sex=a[4];
			     infro.phoneNumber=a[6];
			     infro.address=a[8];
			     infro.e_mail=a[10];
			     list.add(index,infro);
			     index++;
			 }
		br.close();
		reader.close();
    }
}


public class Demo extends JFrame {

    /*
     * �������
     * */
    public Demo(){
        Container c = getContentPane(); //����һ����������c
        JPanel jp = new JPanel();   //�½�JPanel���--jp
        JButton button1 = new JButton("�½���ϵ��");
        JButton button2 = new JButton("ɾ����ϵ��");
        JButton button3 = new JButton("�޸���ϵ��");
        JButton button4 = new JButton("������ϵ��");
        JButton button5 = new JButton("��ʾ��ϵ��");  
        jp.setLayout(new GridLayout(5,1,5,5));//�½���񲼾ֹ�����
        jp.add(button1);
        jp.add(button2);
        jp.add(button3);
        jp.add(button4);
        jp.add(button5);
        c.add(jp);//��JPanel���jp��ӵ���������c��
        setSize(400,250);
        setTitle("*ͨ Ѷ ¼ �� �� ϵ ͳ*");
        setVisible(true);
        setResizable(true);//�����С���ɸı�
        setLocationRelativeTo(null);//������ʾ
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        /*
         *������Ӧ
         * 
         * */
        button1.addActionListener(new ActionListener(){//�½�����ʵ��
            public void actionPerformed(ActionEvent arg0){
                try {
					Infro.addFunction();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        button2.addActionListener(new ActionListener(){//ɾ����ʵ��
            public void actionPerformed(ActionEvent arg0){
                Infro.deleteFunction();
            }
        });
        button3.addActionListener(new ActionListener(){//�޸Ĺ���ʵ��
            public void actionPerformed(ActionEvent arg0){
                try {
					Infro.reditFunction();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        button4.addActionListener(new ActionListener(){//��ѯ����ʵ��
            public void actionPerformed(ActionEvent arg0){
                try {
                    Infro.searchFunction();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        button5.addActionListener(new ActionListener(){//��ʾ����ʵ��
            public void actionPerformed(ActionEvent arg0){
                try {
                    Infro.showFunction();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] args){
    	try {
			Infro.readFunction();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         new Demo();
        Infro a = new Infro("", "", "", "", "");
    }
}